package com.zx.idc.backend.gui.api.management.statusreport.activestate.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zx.idc.backend.gui.api.commom.entity.UploadModel;
import com.zx.idc.backend.gui.api.commom.template.NIdcFtpTemplate;
import com.zx.idc.backend.gui.api.management.statusreport.activestate.service.ActiveStateService;
import com.zx.idc.cache.resource.ServerResourceCacheService;
import com.zx.idc.common.attributes.FTPReportDataTypeEnum;
import com.zx.idc.common.attributes.NetCommunicationFileTypeEnum;
import com.zx.idc.common.attributes.SchemaFileNameEnum;
import com.zx.idc.common.lang.ListUtil;
import com.zx.idc.ds.base.house.entity.BaseHouseInfo;
import com.zx.idc.ds.base.house.service.IBaseHouseInfoService;
import com.zx.idc.ds.base.monitor.entity.BaseMonitorDevice;
import com.zx.idc.ds.base.monitor.service.IBaseMonitorDeviceService;
import com.zx.idc.ds.base.oper.entity.BaseOperCompany;
import com.zx.idc.ds.base.oper.service.IBaseOperCompanyService;
import com.zx.idc.ds.resource.pojo.ServerInfo;
import com.zx.idc.xml.entity.ActiveState;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import static com.zx.idc.common.constants.MonitorDeviceAppStatusConstants.APP_STATUS_RUNNING;

/**
 * Created by hasee on 2019/2/15.
 * ISMS活动状态上报
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ActiveStateServiceImpl implements ActiveStateService {

    private static final String COLUMN_ID = "id";
    private static final String COLUMN_HOST_NAME = "hostName";
    @Autowired
    private IBaseOperCompanyService iBaseOperCompanyService;

    @Autowired
    private IBaseHouseInfoService iBaseHouseInfoService;

    @Autowired
    private NIdcFtpTemplate nIdcFtpTemplate;

    @Autowired
    private ServerResourceCacheService serverResourceCacheService;

    @Autowired
    private IBaseMonitorDeviceService iBaseMonitorDeviceService;

    @Override
    public void activeState() {

        //1.查询所有的ids
        List<BaseOperCompany> baseOperCompanies = iBaseOperCompanyService.listForAll();

        baseOperCompanies.forEach((baseOperCompany) -> {
            ActiveState activeState = new ActiveState();
            //查询idcid下机房总数
            List<BaseHouseInfo> houseInfos = iBaseHouseInfoService.selectList(
                new EntityWrapper<>(
                    new BaseHouseInfo().setIdcId(baseOperCompany.getIdcId())
                ).setSqlSelect(COLUMN_ID)
            );
            int houseNum = houseInfos.size();

            activeState.setIdcId(baseOperCompany.getIdcId());
            activeState.setHouseAmount(houseNum);
            //计算异常机房信息
            List<BaseMonitorDevice> baseMonitorDevices = houseInfos.stream()
                .flatMap(houseInfo ->
                    iBaseMonitorDeviceService.selectList(
                        new EntityWrapper<>(
                            new BaseMonitorDevice().setHouseId(houseInfo.getId())
                        ).setSqlSelect(COLUMN_HOST_NAME)
                    ).stream()
                ).map(baseMonitorDevice -> {
                    ServerInfo serverInfo = serverResourceCacheService.getServerInfo(baseMonitorDevice.getHostName());
                    if (serverInfo != null && !APP_STATUS_RUNNING.equals(serverInfo.getAppStatus())) {
                        return baseMonitorDevice;
                    } else {
                        return null;
                    }
                })
                .filter(ObjectUtils::allNotNull)
                .collect(Collectors.toList());
            if (ListUtil.isNotEmpty(baseMonitorDevices)) {
                activeState.setErrHouseAmount(baseMonitorDevices.size());
            } else {
                activeState.setErrHouseAmount(0);
            }
            activeState.setTimeStamp(LocalDateTime.now());

            nIdcFtpTemplate.uploadXMLFile(
                new UploadModel<>(
                    activeState,
                    SchemaFileNameEnum.ISMS_ACTIVE_STATUS,
                    FTPReportDataTypeEnum.FTP_ISMS_ACTIVE_STATUS,
                    NetCommunicationFileTypeEnum.ACTIVE_STATUS
                )
            );
        });
    }
}
